import { Component, OnInit } from '@angular/core';
import {adSystemApiUrl} from '../../../config/api';
import {Router, ActivatedRoute} from '@angular/router';
import {HttpService} from '../../lib/service/http.service'

@Component({
  selector: 'app-news-detail',
  templateUrl: './news-detail.component.html',
  styleUrls: ['./news-detail.component.scss']
})
export class NewsDetailComponent implements OnInit {

  detailData = {
    author: {nick_name: ''},
    last_article: '',
    next_article: '',
    title: '',
    created_at: ''
  };
  categoryID = 0;
  newestInfo = [];
  classifyList = [];


  constructor(
    private httpService: HttpService,
    private route: ActivatedRoute,
    private router: Router
  ) { }

  ngOnInit() {
    this.route.queryParams.subscribe(
      res => {
        this.categoryID = +res.categoryId||0;
      }
    );
    this.getDetail();
    this.getAllArtical();
    this.getClassifyList();
  }

  // 获取详情资讯
  getDetail(articleId?: number){
    let id = articleId||+this.route.snapshot.params['id'];
    const url = `${adSystemApiUrl}/v1/fddata/posts/detail?postID=${id}`;
    return this.httpService.httpGetAuth(url).subscribe(
      data => {
        this.detailData = data.data;
        document.getElementsByClassName("news-detail-warp")[0].innerHTML = data.data.body;
      }
    )
  }

  // 获取所有最新资讯列表
  getAllArtical() {
    let url = `${adSystemApiUrl}/v1/fddata/posts/list?page=1&pageSize=3`;
    return this.httpService.httpGetAuth(url).subscribe(
      data => {
        this.newestInfo = data.data.data;
      }
    );
  }

  // 获取分类列表
  getClassifyList() {
    const url = `${adSystemApiUrl}/v1/fddata/category/list`;
    return this.httpService.httpGetAuth(url).subscribe(
      data => {
        this.classifyList = data.data;
      }
    );
  }

  // 点击最新资讯，更改内容详情
  changeDetail(data: any) {
    this.categoryID = data.category_id;
    this.getDetail(data.id);
  }

  // 点击分类，跳转至新闻列表
  turnNewsList(categoryId: number) {
    this.router.navigate(['/home/news-list'], {queryParams: {categoryId: categoryId}});
  }
}
